<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./css/template.css" />
        <link type="text/css" rel="stylesheet" href="./css/SyntaxHighlighter.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Start with Java applet</title>
        <script language="javascript" src="./js/shInit.js" />
        <script language="javascript" src="./js/shCore.js" />
        <script language="javascript" src="./js/shBrushCpp.js" />
        <script language="javascript" src="./js/shBrushCSharp.js" />
        <script language="javascript" src="./js/shBrushCss.js" />
        <script language="javascript" src="./js/shBrushDelphi.js" />
        <script language="javascript" src="./js/shBrushJava.js" />
        <script language="javascript" src="./js/shBrushJScript.js" />
        <script language="javascript" src="./js/shBrushPhp.js" />
        <script language="javascript" src="./js/shBrushPython.js" />
        <script language="javascript" src="./js/shBrushRuby.js" />
        <script language="javascript" src="./js/shBrushSql.js" />
        <script language="javascript" src="./js/shBrushVb.js" />
        <script language="javascript" src="./js/shBrushXml.js" />
        <keywords>applet</keywords>
        <author>Julie Muesser</author>
    </head>
    <body>
        <div class="chapter">
            <h2>1. Introduction</h2>
            <p>
            	The aim of this article is to present you java applets. At the end of this tutorial you'll be able to create an applet and insert it to your own webpage.
                <br />
                Java language permits you to conceive two kinds of program : autonomous applications and applets.
                <br />
                <br />
                An autonomous application is a classical application which execute itself under the direct control of the operating system.
                <br />
                An applet is an application which is loaded by a browser (or an appletviewer) and which run under the control of this browser.
                <br />
                <br />
                An applet is a Java program running on an webpage. This is bytecodes that you download and that work on your own computer. To execute an applet you have to get an internet 
                browser (for instance Internet Explorer or Netscape).
            </p>
        </div>
        <div class="chapter">
            <h2>2. Applet life cycle</h2>
            <div class="subChapter">
                <h3>2.1. Presentation</h3>
                <p>
                	To create an applet you have to create a class which inherits the java.applet.Applet class and override its methods. Applets don't have a main function. They use methods which are 
                	called by the browser when an event is triggered.
                    <br />
                    <br />
                    This class will be the applet class. Its name have to be on the 
                    <b>&lt;APPLET&gt;</b> tag field of the HTML page. The class methods allow to :
                </p>
                <ul>
                	
                    <li>Back up and manage parameters</li>
                    <li>Get pictures</li>
                    <li>Get and play with sound sequences</li>
                    <li>Interact with the browser</li>
                    <li>Manage life cycle of the applet events</li>
                </ul>
            </div>
            <div class="subChapter">
                <h3>2.2. What does the browser ?</h3>
                <p>
                	A browser manages the loaded event thanks to a stack. At the top of the stack we find the document which is processing by the browser. That means the page which is displayed by the browser.
                    <br />
                    A HTML page will have two states :
                </p>
                <ul>
                    <li>
                        <b>Active</b> : It is at the top of the stack
                    </li>
                    <li>
                        <b>Inactive</b> : It is not at the top
                    </li>
                </ul>
                <p>When the browser detects that in the active page there is the mark &lt;APPLET CODE=  " Class name "&gt; it loads pseudo code of the specified class and then try to execute it.
                </p>
                <p>
                    <b>
                        <i>We load the pseudo code</i>
                    </b>
                </p>
                <p>
                	Browsers detecting Java can add class definitions to their code while their execution. This is what is done when an applet class is loaded. We can notice that this is the 
                	Java runtime environment of the browser which execute it. The loaded class becoming integrant part of the operating system.
                    <br />
                    <br />
                    The browser creates an instance of the applet class using the default constructor. This constructor can be override. It is this instance which is reference by the keyword 
                    <i>this</i> in the text of the applet class.
                </p>
                <p>
                    <b>
                        <i>Applet life cycle</i>
                    </b>
                </p>
                <p>Once created, the Applet object can have three states :</p>
                <ul>
                    <li>
                        <b>Initialize</b> : once the object is build, the Java virtual machine apply the 
                        <i>
                            <b>init()</b>
                        </i> methods which render it in the initialize state.
                    </li>
                    <li>
                        <b>Active</b> : if the web page which reference the object is active.
                    </li>
                    <li>
                        <b>Inactive</b> : if the web page which reference the object is inactive
                    </li>
                </ul>
                <p>
                	Programer has the possibility to override these methods.
                    <b>
                        <i>init()</i>
                    </b>
                    ,
                    <b>
                        <i>start()</i>
                    </b>
                    ,
                    <b>
                        <i>stop()</i>
                    </b>
                    and
                    <b>
                        <i>destroy()</i>
                    </b> which are called at every change of state like we can see in the following picture. These methods are inherit by all the child classes of the Applet class. They are empty until we inherit them.
                </p>
                <p>
                    <img src="./images/1.jpg" />
                </p>
                <p>
                    Function 
                    <b>
                        <i>start()</i>
                    </b>
                    and 
                    <b>
                        <i>stop()</i>
                    </b>
                    can be called many times during the applet life cycle (when the browser exit the applet graphic context and come back for instance).
					Function 
                    <b>
                        <i>paint()</i>
                    </b> 
                    take in charge every applet graphic context update. It is called whenever the applet became active.
                </p>
            </div>
            <div class="subChapter">
                <h3>2.3. Functions description</h3>
                <p>
                    <b>void destroy()</b>
                    <br />
                    Function called by the browser when the applet became useless to free resources.
                </p>
                <p>
                    <b>void init()</b>
                    <br />
                    Function called by the browser when the applet is loaded. This function gather all the needed initialization of the applet. It is comparable as a constructor. 
                    An applet have to treat all PARAM values passed in the HTML code and add the user interface component.
                </p>
                <p>
                    <b>void setSize(int width, int height)</b>
                    <br />
                    Change the applet size by fixing a new width and a new height. This function doesn't work correctly with all the browser.
                </p>
                <p>
                    <b>void start()</b>
                    <br />
                    Function called by the browser to ask the applet activation next the init function call. It is called every time the user came back to the page that contains the applet after he browses some other pages.
                </p>
            </div>
        </div>
        <div class="chapter">
            <h2>3. Structure and applet elements</h2>
            <div class="subChapter">
                <h3>3.1. Class extension</h3>
                <p>
                	The Applet class is the last class of a derivation suit. When the programer extends this class to create his own applet he can use a lot of these services.
                    <br />
                    Most of classes wait other classes.
                    <br />
                    <br />
                    Listen another class consist to write a new class which can use the fields and methods defined in the extended class. The extended class is the parent class and the class which inherit is the child class.
                    <br />
                    <br />
                    Another way to tell that is to say that the child class inerhits fields and function of its parent or its parents list. Child classes invoke or recover the inherit methods.
                    This is what we call simple legacy.
                    <br />
                    <br />
                    The following scheme presents all classes which extend the Applet class :
                </p>
                <p>
                    <img src="./images/2.jpg" />
                </p>
            </div>
            <div class="subChapter">
                <h3>3.2. Classes description</h3>
                <p>
                    <b>Object</b> : This is the superclass of all the java classes. In Java, every classes extends the Object class and can use all the mehods which they inherit.
                </p>
                <p>
                    <b>Component</b> : This is the base class of all the graphic objects which can be displayed at screen and can interact with the user.
                </p>
                <p>
                    <b>Container</b> : permit to define components object which contains other components.
                    <br /> :
                    It owns an object list contained in a layout manager which determins their places in the container and their dimensions.
                </p>
                <p>
                    <b>Applet :</b> This class allows to define panel object which can be display in a browser.
                    <br />
                    The applet implements some of methods which can be called by the browser.
                </p>
            </div>
        </div>
        <div class="chapter">
            <h2>4. Easily way to write an applet</h2>
            <div class="subChapter">
                <h3>4.1. Example</h3>
                <p>Here's a very easy applet examplet</p>
                <textarea name="code" class="java">import java.applet.*
					import java.awt.*
					public class hello_world extends Applet {
					Label helloLabel = new Label("Hello World");
					//Function called by the browser when the applet is loaded
					public void init() {
					setBackGround(Color.yellow);
					add(helloLabel) 
					}//init()
					}//class hello_world</textarea>
                <p>
                	To compilate the class an applet file must be suffixed by 
                    <b>.java</b>
                    .
                    <br />
                    The Applet 
                    <b>hello_world</b> will be saved under the following name : 
                    <b>hello_world.java</b>
                    .
                </p>
            </div>
            <div class="subChapter">
                <h3>4.2. Some code source explanation</h3>
                <textarea name="code" class="java">import java.applet.*
					import java.awt.*</textarea>
                <p>
                	These two lines mean that we will use standard libraries.
                
                    <b>java.applet</b> 
                    and 
                    <b>java.awt</b>
                    which contains classes to construct applets and GUI
                </p>
                <textarea name="code" class="java">public class hello_world extends Applet</textarea>
                <p>
                    The 
                    <b>
                        <i>hello_world</i>
                    </b>
                    extends the Applet class.
                </p>
                <textarea name="code" class="java">Label helloLabel = new Label("Hello World");</textarea>
                <p>A label is added to the class and contains "Hello World"</p>
                <textarea name="code" class="java">public void init() {
					setBackGround(Color.yellow);
					add(helloLabel) 
					}//init()</textarea>
                <p>
                The init function is called at the applet creation. Here we set yellow as the applet background color and insert the helloLabel Label.
                </p>
            </div>
            <div class="subChapter">
                <h3>4.3. Applet compilation</h3>
                <p>
                    The compilation is launched by the command 
                    <b>javac</b>
                    followed by the class name
                </p>
                <textarea name="code" class="java">Javac MyProg.java</textarea>
                <p> This instruction launches the file compilation but don't create an executable but semi compiled files.</p>
                <p>
                Once compiled :
                    <b>MonProg.class</b>
                    is destinated to be incorporate in a HTML document.
                </p>
            </div>
        </div>
        <div class="chapter">
            <h2>5. Applet integration in a HTML page</h2>
            <div class="subChapter">
                <h3>5.1. Example</h3>
                <p> Applets are called from a HTML source read by your browser.
                </p>
                <textarea name="code" class="java">
                    test of the hello_world applet
					&lt;\TITLE&gt;
					&lt;\HEAD&gt;
					text
                    <applet code="'hello_world.class'" width="150" height="100" align="left">&lt;\APPLET&gt; text
						&lt;\BODY&gt;
						&lt;\HTML&gt;</applet>
                </textarea>
                <p>HTML marker &lt;APPLET ...&gt;&lt;\APPLET&gt; is the name of the applet to be executed.</p>
            </div>
            <div class="subChapter">
                <h3>5.2. Applet market attributes</h3>
                <p>Here's a list of the attributes that we can use for an Applet marker : 
                </p>
                <table cellpadding="0">
                    <tbody>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p />
                                <h2>ATTRIBUT</h2>
                            </td>
                            <td valign="top">
                                <p />
                                <h2>description</h2>
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>ALT=" ? "</p>
                            </td>
                            <td valign="top">
                                <p>Alternative text which is displayed when the browser is not able to load the java applet
                                </p>
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>ALIGN =?</p>
                            </td>
                            <td valign="top">
                            <P>
                            	Control how the applet must be align with the text
                                <p />
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>HEIGHT = ?</p>
                            </td>
                            <td valign="top">
                                <p>
                                	Control the vertical dimension reserved for the applet
								</p>
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>WIDTH = ?</p>
                            </td>
                            <td valign="top">
                                <p>
                                Control the horizontal dimension reserved for the applet
                                </p>
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>HSPACE = ?</p>
                            </td>
                            <td valign="top">
                                <p>
                                	Add additional space at left or right of the applet, between the applet spaces and the text.
                                </p>
                            </td>
                        </tr>
                        <tr bgcolor="#CCCCFF">
                            <td valign="top">
                                <p>VSPACE = ?</p>
                            </td>
                            <td valign="top">
                                <p>
                                	Add additional space at top or bottom of the applet, between the applet spaces and the text.
                                </p>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <p>
                	You can try to replace in your HTML page the value left of the ALIGN attribute by values 
                    <b>right</b>
                    ,
                    <b>top</b>
                    or
                    <b>bottom</b> by testing the effect made on your HTML page thanks to your browser after every update.
                </p>
            </div>
        </div>
    </body>
</html>

